package com.garrett.demo.sys.dao;

import com.garrett.demo.sys.entity.Account;
import com.garrett.demo.sys.entity.SimsStudent;

public class UserDataBaseDemo implements UserDao{

    private SimsStudent[] students=new SimsStudent[3];

    private Account[] accounts=new Account[3];

    {
        students[0]=new SimsStudent("212006392","赖子炜","352227200203011316",
                "13489999646","男","1542832219@qq.com");
        students[1]=new SimsStudent("admin","admin","null",
                "null","null","null");
        accounts[0]=new Account("1","212006392","Laiziwei");
        accounts[1]=new Account("2","admin","admin");
    }

    // 根据学号获取学生信息
    @Override
    public SimsStudent getStudentById(String stuId){
        for (SimsStudent student : students) {
            if (student.getStudentId().equals(stuId))
                return student;
        }
        return null;
    }




    //根据登录账号获取账户资源信息
    @Override
    public Account getUserByAccount(String account){
        for (Account value : accounts) {
            if (value.getAccount().equals(account))
                return value;
        }
        return null;
    }
    //注册账户信息
    @Override
    public int addAccount(Account account){
        for(int i=0;i<accounts.length;i++)
            if(accounts[i]==null){
                for(int j=0;j<i;j++) {
                    if (accounts[j] == account) {
                        return 0;
                    }
                }
                accounts[i] = account;
                return 1;
            }
        return 0;
    }

    //添加用户信息
    @Override
    public int createUser(SimsStudent userInfo){
        for(int i=0;i<students.length;i++)
            if(students[i]==null){
                students[i]=userInfo;
                return 1;
            }
        return 0;
    }

    //获取所有学生信息
    @Override
    public SimsStudent[] getAllStudent(){
        return students;
    }



    //--------------------实训二添加部分
    /**
     * 删除账户
     * @param id 主键id - 不是学号
     * @return 1-成功 0-失败
     */
    //删除账户
    @Override
    public int delAccountById(String id){
        for(int i=0;i<accounts.length  && accounts[i]!=null;i++){
            if(accounts[i].getId().equals(id)){
                accounts[i]=null;
                return 1;
            }
        }
        return 0;
    }


    /**
     * 修改账户信息
     * @param account 主键id不可改变，根据主键id判断修改。
     *                <p>例子：{id:23,account:212006677,password:admin}</p>
     *                <p>根据id判断数组中是否存在对应id值，如存在则可以修改学号和密码，否则无效修改</p>
     * @return 1-成功 0-失败
     */
    //修改账户信息
    @Override
    public int modifyAccount(Account account){
        for(int i=0;i<accounts.length && accounts[i]!=null;i++){
            if(accounts[i].getId().equals(account.getId())){
                if(account.getAccount()!=null) {
                    accounts[i].setAccount(account.getAccount());
                }else if(account.getPassword()!=null){
                    accounts[i].setPassword(account.getPassword());
                }
                return 1;
            }
        }
        return 0;
    }


    /**
     * 删除学生信息
     * @param stuId 学号
     * @return
     */
    //删除学生信息
    @Override
    public int delStudentInfo(String stuId){
        for(int i=0;i<students.length && accounts[i]!=null;i++){
            if(students[i].getStudentId().equals(stuId)){
                students[i]=null;
                return 1;
            }
        }
        return 0;
    }


    /**
     * 修改学生信息
     *
     * <p>studentId(学号)不可改变，根据主键studentId判断修改</p>
     * <p>例子：{studentId:212006677,studentName:garrett,email:null}</p>
     * <p>根据studentId判断数组中是否存在对应学号，如存在则可以修改除学号以外的对应属性值，否则无效修改</p>
     *
     * @param student 学生信息
     * @return 1-成功 0-失败
     */
    //修改学生信息
    @Override
    public int modifyStudentInfo(SimsStudent student){
        for(int i=0;i<students.length &&students[i]!=null;i++){
            if(students[i].getStudentId().equals(student.getStudentId())){
                if(students[i].getStudentName()!=null){
                    students[i].setStudentName(student.getStudentName());
                }else if(students[i].getIdCardNo()!=null){
                    students[i].setIdCardNo(student.getIdCardNo());
                }else if(students[i].getMobilePhone()!=null){
                    students[i].setMobilePhone(student.getMobilePhone());
                }else if(students[i].getGender()!=null){
                    students[i].setGender(student.getGender());
                }else if(students[i].getEmail()!=null) {
                    students[i].setEmail(student.getEmail());
                }
                return 1;
            }
        }
        return 0;
    }

    //获取所有账户信息
    public Account[] getAllAccount(){
        return accounts;
    }


    //获取学生信息的长度
    public int studentsLength(){
        return students.length;
    }

    // 根据id获取账户信息
    public Account getAccountById(String id){
        for (Account student : accounts) {
            if (student.getId().equals(id))
                return student;
        }
        return null;
    }



}
